package com.jimster.plugins.rss
{
	import com.bedrock.framework.Bedrock;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class RssFeed extends EventDispatcher
	{
		/*
		Variable Declarations
		*/
		private static var __instance:RssFeed;
		private var strRssPath:String;
		private var ldrRss:URLLoader;
		private var rawXmlData:XML;
		private var objData:Object;
		
		public static const RSS_READY:String = "RssRaedy";
		
		//Accesor & getters
		public function get data():Object{ return this.objData };
		
		/*
		Constructor
		*/
		public function RssFeed( $singletonEnforcer:SingletonEnforcer )
		{
		}
		public static function getInstance():RssFeed
		{
			if (RssFeed.__instance == null) {
				RssFeed.__instance = new RssFeed( new SingletonEnforcer );
			}
			return RssFeed.__instance;
		}
		
		public function initialize( $rssPath:String ):void
		{
			this.strRssPath = $rssPath;
			this.loadRss();
		}
		/*
		Public methods
	 	*/
	 	
		/*
		Private methods
		*/
		private function loadRss():void
		{
			this.ldrRss = new URLLoader();
			this.ldrRss.addEventListener( Event.COMPLETE, this.handleRssLoad );
			this.ldrRss.load( new URLRequest( this.strRssPath ) );
		}
		
		/*
		Event Handlers
		*/
	 	private function handleRssLoad( $event:Event ):void
		{
			this.rawXmlData = XML( $event.target.data );
			this.objData = Object( this.rawXmlData.channel  );
			this.dispatchEvent( new Event( RSS_READY ) );
		}
	
	}
}
/*
This private class is only accessible by the public class.
The public class will use this as a 'key' to control instantiation.   
*/
class SingletonEnforcer {}